package com.ehotting.edsta.circuitDesign.service.impl;

import com.alibaba.fastjson.JSON;
import com.ehotting.eaf.netty.domain.DataAble;
import com.ehotting.edsta.circuitDesign.controller.CircuitDesignWsController;
import com.ehotting.mqbd.api.bean.MQDataAble;
import com.ehotting.mqbd.core.mq.consumer.ClientMQDataHandlerAble;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CircuitClientMQDataHandler extends ClientMQDataHandlerAble {

    private static Logger logger = LoggerFactory.getLogger(CircuitClientMQDataHandler.class);

    @Autowired
    private CircuitDesignWsController wsController;

    @Override
    public void invoke(MQDataAble mqData) {

        log.info("response:{}",JSON.toJSONString(mqData.getResponse()));
        String agentId = mqData.getAgentId();
        log.info("agentId:{}",agentId);
        if(mqData.getResponse()==null){
            log.info("mqData.getResponse() is null,return.",mqData.getResponseString());
            return;
        }

        invoke( mqData.getAgentId(),  (DataAble)mqData.getResponse());
    }

    @Override
    public void invoke(String agentId, DataAble response) {
        try {
            logger.info("agentId:{},response:{}",agentId,JSON.toJSONString(response));
            wsController.sendUserMessage(JSON.toJSONString(response),agentId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
